package com.right.dao.impl;

import java.sql.Connection;
import java.util.List;

import com.Tools.DBTools;
import com.Tools.PageData;
import com.right.dao.OrdersDao;
import com.right.entity.Orders;

/**
 * 订单Dao接口实现类
 * 
 * @author 柯富峰
 */
public class OrdersDaoImpl implements OrdersDao {
	Connection conn = DBTools.getConn();

	/**
	 * 增加订单
	 * 
	 * @param orders 订单对象
	 * @return 操作成功;false 操作失败
	 */
	@Override
	public boolean addOrders(Orders orders) {
		String sql = "insert into orders values(0,?,?,?,?,now(),null,?,?,?,?)";
		return DBTools.exUpdate(sql, orders.getStore(), orders.getTotal(), orders.getAddress(), orders.getOrderstatus(),
				orders.getUserid(), orders.getUsercall(), orders.getStorecall(), orders.getOrderinfo()) > 0;
	}

	/**
	 * 增加订单
	 * 
	 * @param conn   连接
	 * @param orders 订单对象
	 * @return n=0 添加失败;n>0 添加成功
	 */
	@Override
	public int addOrders(Connection conn, Orders orders) {
		String sql = "insert into orders values(?,?,?,?,?,now(),null,?,?,?,?)";
		int n = 0;
		try {
			n = DBTools.exUpdate(sql, conn, orders.getOrderid(), orders.getStore(), orders.getTotal(),
					orders.getAddress(), orders.getOrderstatus(), orders.getUserid(), orders.getUsercall(),
					orders.getStorecall(), orders.getOrderinfo());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return n;
	}

	/**
	 * 删除订单
	 * 
	 * @param orders 订单对象
	 * @return true 操作成功;false 操作失败
	 */
	@Override
	public boolean delOrders(Orders orders) {
		String sql = "delete from orders where orderid = ?";
		return DBTools.exUpdate(sql, orders.getOrderid()) > 0;
	}

	/**
	 * 修改订单(收货地址)
	 * 
	 * @param orders 订单对象
	 * @return 操作成功;false 操作失败
	 */
	@Override
	public boolean updateOrdersByAddress(Orders orders) {
		String sql = "update orders set orderstatus =? where orderid = ?";

		return DBTools.exUpdate(sql, orders.getAddress(), orders.getOrderid()) > 0;
	}

	/**
	 * 修改订单(订单状态)
	 * 
	 * @param orders 订单对象
	 * @return 操作成功;false 操作失败
	 */
	@Override
	public boolean updateOrdersByOrderstatus(Orders orders) {
		String sql = "update orders set orderstatus =? where orderid = ?";

		return DBTools.exUpdate(sql, orders.getOrderstatus(), orders.getOrderid()) > 0;
	}

	/**
	 * 修改订单(订单备注)
	 * 
	 * @param orders 订单对象
	 * @return 操作成功;false 操作失败
	 */
	@Override
	public boolean updateOrdersByOrderinfo(Orders orders) {
		String sql = "update orders set orderinfo =? where orderid = ?";

		return DBTools.exUpdate(sql, orders.getOrderinfo(), orders.getOrderid()) > 0;
	}

	/**
	 * 修改订单(送达时间)
	 * 
	 * @param orders 订单对象
	 * @return 操作成功;false 操作失败
	 */
	@Override
	public boolean updateOrdersByEndtime(Orders orders) {
		String sql = "update orders set endtime = now() where orderid = ?";

		return DBTools.exUpdate(sql, orders.getOrderid()) > 0;
	}

	/**
	 * 查询订单信息(店家)
	 * 
	 * @param store 店家
	 * @return null 查无信息;list 查询结果
	 */
	@Override
	public List<Orders> getOrdersByStore(String store) {
		String sql = "select orderid, store, total, address, orderstatus, starttime, endtime, userid, usercall, storecall, orderinfo from orders where store = ?";
		return (List<Orders>) DBTools.exQuery(sql, Orders.class, store);
	}

	/**
	 * 查询订单信息(店家)分页
	 * 
	 * @param page     页数
	 * @param pageSize 单页数据
	 * @param store    店家
	 * @return null 查无信息;list 查询结果
	 */
	@Override
	public PageData<Orders> getOrdersByStorePage(int page, int pageSize, String store) {
		String sql = "select orderid, store, total, address, orderstatus, starttime, endtime, userid, usercall, storecall, orderinfo from orders where store = ?";
		PageData<Orders> pd = DBTools.exQueryByPage(sql, Orders.class, page, pageSize, store);
		return pd;
	}

	/**
	 * 查询订单信息(用户编号)
	 * 
	 * @param userid 用户编号
	 * @return null 查无信息;list 查询结果
	 */
	@Override
	public List<Orders> getOrdersByUserid(int userid) {
		String sql = "select orderid, store, total, address, orderstatus, starttime, endtime, userid, usercall, storecall, orderinfo from orders where userid = ?";
		return (List<Orders>) DBTools.exQuery(sql, Orders.class, userid);
	}

	/**
	 * 查询订单信息(用户编号)分页
	 * 
	 * @param page     页数
	 * @param pageSize 单页数据
	 * @param userid   用户编号
	 * @return null 查无信息;list 查询结果
	 */
	@Override
	public PageData<Orders> getOrdersByUseridPage(int page, int pageSize, int userid) {
		String sql = "select orderid, store, total, address, orderstatus, starttime, endtime, userid, usercall, storecall, orderinfo from orders where userid = ?";
		PageData<Orders> pd = DBTools.exQueryByPage(sql, Orders.class, page, pageSize, userid);
		return pd;
	}

	/**
	 * 查询订单信息(订单状态关键字)
	 * 
	 * @param keywords 订单状态关键字
	 * @return null 查无信息;list 查询结果
	 */
	@Override
	public List<Orders> getOrdersByOrderstatus(String keywords) {
		String sql = "select orderid, store, total, address, orderstatus, starttime, endtime, userid, usercall, storecall, orderinfo from orders where orderstatus like ?";
		return (List<Orders>) DBTools.exQuery(sql, Orders.class, '%' + keywords + "%");
	}

	/**
	 * 查询订单信息(订单状态关键字)分页
	 * 
	 * @param page     页数
	 * @param pageSize 单页数据
	 * @param keywords 订单状态关键字
	 * @return null 查无信息;list 查询结果
	 */
	@Override
	public PageData<Orders> getOrdersByOrderstatusPage(int page, int pageSize, String keywords) {
		String sql = "select orderid, store, total, address, orderstatus, starttime, endtime, userid, usercall, storecall, orderinfo from orders where orderstatus = ?";
		PageData<Orders> pd = DBTools.exQueryByPage(sql, Orders.class, page, pageSize, '%' + keywords + "%");
		return pd;
	}

	/**
	 * 查询订单信息(下单时间)
	 * 
	 * @param starttime 下单时间
	 * @return null 查无信息;list 查询结果
	 */
	@Override
	public List<Orders> getOrdersByStarttime(String starttime) {
		String sql = "select orderid, store, total, address, orderstatus, starttime, endtime, userid, usercall, storecall, orderinfo from orders where starttime = like ?";
		return (List<Orders>) DBTools.exQuery(sql, Orders.class, '%' + starttime + "%");
	}

	/**
	 * 查询订单信息(下单时间)分页
	 * 
	 * @param page      页数
	 * @param pageSize  单页数据
	 * @param starttime 下单时间
	 * @return null 查无信息;list 查询结果
	 */
	@Override
	public PageData<Orders> getOrdersByStarttimePage(int page, int pageSize, String keywords) {
		String sql = "select orderid, store, total, address, orderstatus, starttime, endtime, userid, usercall, storecall, orderinfo from orders where store = ?";
		PageData<Orders> pd = DBTools.exQueryByPage(sql, Orders.class, page, pageSize, '%' + keywords + "%");
		return pd;
	}

	/**
	 * 查询订单信息(订单编号)
	 * 
	 * @param store 店家
	 * @return null 查无信息;list 查询结果
	 */
	@Override
	public List<Orders> getOrdersByOrderid(String orderid) {
		String sql = "select orderid, store, total, address, orderstatus, starttime, endtime, userid, usercall, storecall, orderinfo from orders where orderid = ?";
		return (List<Orders>) DBTools.exQuery(sql, Orders.class, orderid);
	}

	@Override
	public PageData<Orders> getOrdersByStoreByPage(int page, int pageSize, String keywords) {
		String sql = "select orderid, store, total, address, orderstatus, starttime, endtime, userid, usercall, storecall, orderinfo from orders where store like ?";
		PageData<Orders> pd = DBTools.exQueryByPage(sql, Orders.class, page, pageSize, '%' + keywords + "%");
		return pd;
	}

	@Override
	public boolean delOrdersById(String orderid) {
		// TODO Auto-generated method stub
		String sql = "delete from orders where orderid = ?";
		return DBTools.exUpdate(sql, orderid) > 0;
	}

	@Override
	public boolean updateOrders(Orders orders) {
		// TODO Auto-generated method stub
		if (orders == null) {
			return false;
		}
		// TODO Auto-generated method stub
		String sql = "update orders set store=?,total=?,address=?,orderstatus=?,starttime=? userid=? usercall=? storecall=? orderinfo=? where orderid = ?";
		return DBTools.exUpdate(sql, orders.getStore(), orders.getTotal(), orders.getAddress(), orders.getOrderstatus(),
				orders.getStarttime(), orders.getUserid(), orders.getUsercall(),
				orders.getStorecall(), orders.getOrderinfo(), orders.getOrderid()) > 0;
	}

}
